package generator

import (
	v1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"

	"v8s-controller/internal/configuration"
)

type (
	ResourceQuotaGenerator struct {
		defaultCPULimit         resource.Quantity
		defaultRAMLimit         resource.Quantity
		defaultEphemeralStorage resource.Quantity
		properties              configuration.GeneratorProperties
	}
)

const (
	defaultResourceQuotaName = "quota"
)

var (
	ResourceQuotaMeta = metav1.TypeMeta{
		Kind:       "ResourceQuota",
		APIVersion: "v1",
	}
)

func NewResourceQuotaGenerator(properties configuration.GeneratorProperties) *ResourceQuotaGenerator {
	return &ResourceQuotaGenerator{
		defaultCPULimit:         resource.MustParse(properties.Quota.CPULimit),
		defaultRAMLimit:         resource.MustParse(properties.Quota.RAMLimit),
		defaultEphemeralStorage: resource.MustParse(properties.Quota.EphemeralStorageLimit),
		properties:              properties,
	}
}

func (g *ResourceQuotaGenerator) Generate() []runtime.Object {
	return []runtime.Object{
		&v1.ResourceQuota{
			TypeMeta: ResourceQuotaMeta,
			ObjectMeta: metav1.ObjectMeta{
				Name: defaultResourceQuotaName,
			},
			Spec: v1.ResourceQuotaSpec{
				Hard: map[v1.ResourceName]resource.Quantity{
					v1.ResourceLimitsCPU:              g.defaultCPULimit,
					v1.ResourceLimitsMemory:           g.defaultRAMLimit,
					v1.ResourceLimitsEphemeralStorage: g.defaultEphemeralStorage,
				},
			}},
	}
}
